package com.goldgov.commylog.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.branchCircle.service.BranchCircle;
import com.goldgov.commylog.dao.CommylogDao;
import com.goldgov.commylog.service.Commylog;
import com.goldgov.commylog.service.CommylogQuery;
import com.goldgov.commylog.service.CommylogService;
import com.goldgov.commylog.service.StatisticsCommylogBean;
import com.goldgov.commylog.service.StatisticsCommylogQuery;
import com.goldgov.utils.PropertyUtil;

@Service
public class CommylogServiceImpl implements CommylogService {
	
	@Autowired
	private CommylogDao dao;
	
	@Override
	public Commylog getCommylogID(){
		return dao.getCommylogID();
	}
	
	@Override
	public String addCommylog(Commylog obj){
		dao.addCommylog(obj);
		return null;
	}
	
	@Override
	public int deleteCommylog(String[] ids){
		return dao.deleteCommylog(ids);
	}
	
	@Override
	public int updateCommylog(Commylog obj){
		return dao.updateCommylog(obj);
	}
	
	@Override
	public void preUpdateCommylogByAdmin(Commylog obj){
		dao.preUpdateCommylogByAdmin(obj);
	}
	
	@Override
	public void updateCommylogByAdmin(Commylog obj){
		dao.updateCommylogByAdmin(obj);
	}
	
	@Override
	public Commylog getCommylog(String id){
		return dao.getCommylog(id);
	}
	
	@Override
	public List<Commylog> listCommylog(CommylogQuery query){
		return dao.listCommylog(query);
	}
	
	@Override
	public List<Commylog> listCommylogByAdmin(CommylogQuery query){
		return dao.listCommylogByAdmin(query);
	}

	@Override
	public void updateCommylogByID(Commylog obj) {
		dao.updateCommylogByID(obj);
	}

	@Override
	public Commylog getUserInfo(String userID) {
		return dao.getUserInfo(userID);
	}

	@Override
	public List<Commylog> findOrgUser(CommylogQuery query) {
		return dao.findOrgUser(query);
	}
	@Override
	public List<StatisticsCommylogBean> statisticsTxl(StatisticsCommylogQuery query) {
		query.setPageSize(-1);
		if (!PropertyUtil.objectNotEmpty(query.getSearchStartDate())) {
			query.setSearchStartDate(null);
		}
		if (!PropertyUtil.objectNotEmpty(query.getSearchEndDate())) {
			query.setSearchEndDate(null);
		}
		List<StatisticsCommylogBean> list=dao.statisticsTxl(query);
		if(Integer.valueOf(2).equals(query.getSearchOrderType())) {
			//按完成度排序
			List<StatisticsCommylogBean> list2=list.stream()
					.sorted(Comparator.comparing(StatisticsCommylogBean::getWriteRate).reversed())
					.collect(Collectors.toList());
			return list2;
		}else {
			//按组织排序
			return list;
		}
	}
	@Override
	public List<StatisticsCommylogBean> statisticsTxlZB(StatisticsCommylogQuery query) {
		query.setPageSize(-1);
		if (!PropertyUtil.objectNotEmpty(query.getSearchStartDate())) {
			query.setSearchStartDate(null);
		}
		if (!PropertyUtil.objectNotEmpty(query.getSearchEndDate())) {
			query.setSearchEndDate(null);
		}
		List<StatisticsCommylogBean> list=dao.statisticsTxlZB(query);
		if(Integer.valueOf(2).equals(query.getSearchOrderType())) {
			//按完成度排序
			List<StatisticsCommylogBean> list2=list.stream()
					.sorted(Comparator.comparing(StatisticsCommylogBean::getWriteRate).reversed())
					.collect(Collectors.toList());
			return list2;
		}else {
			//按组织排序
			return list;
		}
	}
	@Override
	public List<StatisticsCommylogBean> statisticsSyl(StatisticsCommylogQuery query) {
		query.setPageSize(-1);
		if (!PropertyUtil.objectNotEmpty(query.getSearchStartDate())) {
			query.setSearchStartDate(null);
		}
		if (!PropertyUtil.objectNotEmpty(query.getSearchEndDate())) {
			query.setSearchEndDate(null);
		}
		
		List<StatisticsCommylogBean> list=dao.statisticsSyl(query);
		if(Integer.valueOf(2).equals(query.getSearchOrderType())) {
			List<StatisticsCommylogBean> list2=new ArrayList<>();
				//根据avgReachNum从大到小排序
				list2=list.stream()
						.sorted(Comparator.comparing(StatisticsCommylogBean::getAuditRate).reversed())
						.collect(Collectors.toList());
			return list2;
		}
		return list;
	}
	@Override
	public List<StatisticsCommylogBean> statisticsSylZB(StatisticsCommylogQuery query) {
		query.setPageSize(-1);
		if (!PropertyUtil.objectNotEmpty(query.getSearchStartDate())) {
			query.setSearchStartDate(null);
		}
		if (!PropertyUtil.objectNotEmpty(query.getSearchEndDate())) {
			query.setSearchEndDate(null);
		}
		return dao.statisticsSylZB(query);
	}

	/**
	 * 查询下一级组织日志数量
	 * @date 2018年12月12日 下午4:41:28 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	@Override
	public List<StatisticsCommylogBean> findOrgCommylogNum(StatisticsCommylogQuery query){
		return dao.findOrgCommylogNum(query);
	}
	
}